

# from ctypes import *
# from itertools import count
# import cv2
# SHM_SIZE = 1920*1080*3
# SHM_KEY = 65000

# def ku():
#     try:
#         rt = CDLL('librt.so')
#     except:
#         rt = CDLL('librt.so.1')

#     shmget = rt.shmget
#     shmget.argtypes = [c_int, c_size_t, c_int]
#     shmget.restype = c_int
#     shmat = rt.shmat
#     shmat.argtypes = [c_int, POINTER(c_void_p), c_int]
#     shmat.restype = c_void_p

#     shmid = shmget(SHM_KEY, SHM_SIZE, 0o666)
#     if shmid < 0:
#         print ("System not infected")
#     else:
#         addr = shmat(shmid, None, 0)
#     import numpy as np

#     while 1:
#         print(shmid)
#         nparr = np.frombuffer(string_at(addr,20),dtype= np.uint32,count= 2)
#         print(nparr)
#         img = np.frombuffer(string_at(addr+8,nparr[0]*nparr[1]*3),dtype= np.uint8,count= nparr[0]*nparr[1]*3).reshape(nparr[0],nparr[1],3)
#         cv2.imshow("a",img)
#         cv2.waitKey(1000)
#         #

#         # print(nparr)
#         # break
#             # image_data = np.asarray(image, dtype=np.uint8)
#             # image_data = image_data.ctypes.data_as(ctypes.c_void_p)


#!/usr/bin/env python

import sys

# sys.path.append("/usr/lib/python3/dist-packages")
# sys.path.append("/home/frank/Documents/215/code/parrot-groundsdk/.python/py3/lib/python3.5/site-packages")

import cv2
import ctypes
import numpy as np


class getRGB():
    def __init__(self) -> None:
        ll = ctypes.cdll.LoadLibrary
        self.lib = ll("/project/train/y3/build/libmain.so")
        self.lib.dump_.restype = ctypes.c_int
        self.lib.read_.restype = ctypes.c_ulong

    def read(self):
        value = self.lib.read_(0)
        sz = np.frombuffer(ctypes.string_at(value, 4*2),
                           dtype=np.uint32, count=2)
        image = np.frombuffer(ctypes.string_at(
            value+4*2, sz[0]*sz[1]*3), dtype=np.uint8, count=3*sz[0]*sz[1]).reshape(sz[0], sz[1], 3)
        return image

    def send(self, image):
        image_data = image.ctypes.data_as(ctypes.c_void_p)
        value = self.lib.dump_(1, image.shape[0], image.shape[1], image_data)


if __name__ == '__main__':
    import ji
    model = ji.Mymodel()
    g = getRGB()
    while(1):
        rgb = g.read()
        mask = model(rgb)
        g.send(mask)
        print("main send mask")
        # ji.process_image()
